package com.example.baiduapi.Controller.txtController;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.baiduapi.Mapper.mysqlMapper.MessageMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Controller
@CrossOrigin
@EnableAsync
public class GetTxt {
    @Resource
    MessageMapper messagesMapper;
    @PostMapping("/vivo50")
    @ResponseBody
    public String vivo50(){
        String s = HttpUtil.get("https://api.shadiao.pro/kfc");
        // 解Unicode
        String unicode = JSON.parseObject(s).getString("data");
        Map mapData = JSON.parseObject(unicode, Map.class);
        Object result = mapData.get("text");
        return String.valueOf(result);
    }

    @PostMapping("/tiandog")
    @ResponseBody
    public String tiandog(){
        String s = HttpUtil.get("https://v2.api-m.com/api/dog");
        Map mapData = JSON.parseObject(s, Map.class);
        Object result = mapData.get("data");
        return String.valueOf(result);
    }


    @PostMapping("/addtiandog")
    @ResponseBody
    public void addTiandog() throws ExecutionException, InterruptedException {
        System.out.println(new Date());
        String s1 = add().get();
        String s2 = add1().get();
        String s3 = add2().get();
        System.out.println(s3);
        System.out.println("s1返回："+s1);
        System.out.println("s2返回："+s2);
        System.out.println("s3返回："+s3);
        System.out.println(new Date());
    }
    @PostMapping("/addtiandog2")
    @ResponseBody
    public void addTiandog2(){
        System.out.println(new Date());
        String url = "http://localhost:8888/addtiandog2";
        HttpUtil.get(url);
        System.out.println(new Date());
    }
    @Async
    public CompletableFuture<String> add(){
        String s = HttpUtil.get("https://api.shadiao.pro/kfc");
        // 解Unicode
        String unicode = JSON.parseObject(s).getString("data");
        Map mapData = JSON.parseObject(unicode, Map.class);
        Object result = mapData.get("text");
        Integer i = messagesMapper.countMessage(String.valueOf(result));
        if (i == 0) {
            messagesMapper.insertTDmessage("2",String.valueOf(result));
        }
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {

        }
        System.out.println(String.valueOf(result));
        return CompletableFuture.completedFuture(String.valueOf(result));
    }@Async
    public CompletableFuture<String> add1(){
        String s = HttpUtil.get("https://api.shadiao.pro/kfc");
        // 解Unicode
        String unicode = JSON.parseObject(s).getString("data");
        Map mapData = JSON.parseObject(unicode, Map.class);
        Object result = mapData.get("text");
        Integer i = messagesMapper.countMessage(String.valueOf(result));
        if (i == 0) {
            messagesMapper.insertTDmessage("2",String.valueOf(result));
        }
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {

        }
        System.out.println(String.valueOf(result));
        return CompletableFuture.completedFuture(String.valueOf(result));
    }@Async
    public CompletableFuture<String> add2(){
        String s = HttpUtil.get("https://api.shadiao.pro/kfc");
        // 解Unicode
        String unicode = JSON.parseObject(s).getString("data");
        Map mapData = JSON.parseObject(unicode, Map.class);
        Object result = mapData.get("text");
        Integer i = messagesMapper.countMessage(String.valueOf(result));
        if (i == 0) {
            messagesMapper.insertTDmessage("2",String.valueOf(result));
        }
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {

        }
        System.out.println(String.valueOf(result));
        return CompletableFuture.completedFuture(String.valueOf(result));
    }

    public static void main(String[] args) {

    }


}
